﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;

namespace HYCodeBase.WPF
{
	public static class WPFUtilityMethod
	{
		public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
		{
			Bitmap bitmap2 = new Bitmap(bitmap.Width, bitmap.Height);
			for (int i = 0; i < bitmap.Width; i++)
			{
				for (int j = 0; j < bitmap.Height; j++)
				{
					System.Drawing.Color pixel = bitmap.GetPixel(i, j);
					System.Drawing.Color color = System.Drawing.Color.FromArgb((int)pixel.R, (int)pixel.G, (int)pixel.B);
					bitmap2.SetPixel(i, j, color);
				}
			}
			MemoryStream memoryStream = new MemoryStream();
			bitmap2.Save(memoryStream, ImageFormat.Bmp);
			BitmapImage bitmapImage = new BitmapImage();
			bitmapImage.BeginInit();
			bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
			bitmapImage.EndInit();
			return bitmapImage;
		}
	}
}
